package com.springcloud.controller;
import com.springcloud.pojo.Dept;
import com.springcloud.service.DeptServiceImpl;
import org.apache.ibatis.javassist.compiler.ast.InstanceOfExpr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

//提供restful服务
@RestController
public class DeptController {

    @Autowired
//    可以获取一些配置信息，得到具体的微服务
    private DiscoveryClient client;

    @Autowired
    DeptServiceImpl deptService;

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept){
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return deptService.queryById(id);
    }

    @GetMapping("/dept/list")
    public List<Dept> addDept(){
        System.out.println("hello world!");
        return deptService.queryAll();
    }

//    获取一些注册中心上的消息
    @GetMapping("/dept/descovery")
    public Object discovery(){
//        获得微服务列表的清单
        List<String> services=client.getServices();
        System.out.println("discovery=>service:"+services);

//        得到一个具体的信息  参数为具体的服务信息
        List<ServiceInstance> instances=client.getInstances("SPRING-CLOUD-PROVIDER-DEPT");
        for(ServiceInstance serviceInstance:instances){
            System.out.println(serviceInstance.getHost()+"\t"+
                    serviceInstance.getUri()+"\t"+
                    serviceInstance.getPort()+"\t"+
                    serviceInstance.getServiceId());
        }
        return this.client;
    }
}
